package cn.stylefeng.guns.modular.demo.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.RandomUtil;
import cn.stylefeng.guns.modular.demo.constant.FileCons;
import cn.stylefeng.guns.modular.demo.service.DemoService;
import cn.stylefeng.guns.modular.demo.utils.DateUtils;
import cn.stylefeng.guns.modular.demo.utils.FileUtil;
import cn.stylefeng.guns.modular.demo.utils.RandomsUtil;
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.system.api.pojo.notice.SysNoticeRequest;
import cn.stylefeng.roses.kernel.system.modular.notice.entity.SysNotice;
import cn.stylefeng.roses.kernel.system.modular.notice.service.impl.SysNoticeServiceImpl;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 培训作业
 */
@RestController
@ApiResource(name = "培训作业")
public class DemoController {

    @Resource
    private DemoService demoService;

    @Resource
    private SysNoticeServiceImpl sysNoticeService;

    /**
     * 文件上传
     * @param file
     */
    @PostResource(name = "文件上传", path = "/demo/uploads")
    public ResponseData<Map<String, Object>> uploads(@RequestPart("file") MultipartFile file) {
        Map<String, Object> maps = demoService.uploads(file);
        return new SuccessResponseData<>(maps);
    }

    /**
     * 文件下载
     * @param request
     */
    @GetMapping(name = "文件下载", path = "/demo/downs")
    public ResponseEntity<byte[]> downs(HttpServletRequest request) throws Exception{
        String path = request.getParameter("path");
        path = path.replace("\\", "/");
        String[] split = path.split("/");
        String saveFileName = split[split.length-1];
        return FileUtil.down(path, saveFileName);
    }

    /**
     * 文件导入
     * @param file
     */
    @PostResource(name = "文件导入", path = "/demo/inports")
    public ResponseData<Map<String, Object>> inports(@RequestPart("file") MultipartFile file) throws IOException {
        Map<String, Object> maps = demoService.uploads(file);
        String filePath = maps.get("file_path")+"";
        Workbook wb = new XSSFWorkbook(filePath);
        Sheet sheet = wb.getSheetAt(0);//参数：索引
        for (int rowNum = 2; rowNum<= sheet.getLastRowNum() ;rowNum ++) {
            Row row = sheet.getRow(rowNum);
            StringBuilder sb = new StringBuilder();
            SysNoticeRequest sysNotice = new SysNoticeRequest();
            for(int cellNum=0;cellNum< row.getLastCellNum(); cellNum ++) {
                Cell cell = row.getCell(cellNum);
                Object value = FileUtil.getCellValue(cell);
                sb.append(value).append("wxwxwx");
                sysNotice.setNoticeId(Long.valueOf(RandomsUtil.getRandom(6)));
            }
            sysNotice.setNoticeContent(sb.toString().split("wxwxwx")[1]);
            sysNotice.setNoticeTitle(sb.toString().split("wxwxwx")[2]);
            sysNoticeService.add(sysNotice);
        }
        return new SuccessResponseData<>(maps);
    }

    /**
     * 文件导出
     * @param request
     */
    @GetMapping(name = "文件导出", path = "/demo/exports")
    public ResponseEntity<byte[]> exports(HttpServletRequest request) throws Exception{
        Workbook wb = new XSSFWorkbook(FileCons.FILE_TEMPLATE);
        List<SysNotice> list = sysNoticeService.list();
        list.removeIf(sysNotice -> sysNotice.getDelFlag().equals("Y"));

        Sheet sheet = wb.getSheetAt(0);
        HashMap<String,CellStyle> cssMap = new HashMap<String, CellStyle>();
        for(int i = 0; i < 100; i++){
            Cell cell = sheet.getRow(2).getCell(i);
            if(null!=cell){
                CellStyle cellStyle = sheet.getRow(2).getCell(i).getCellStyle();
                cssMap.put("css"+i, cellStyle);
            }
        }

        for(int i = 0;i<list.size() ;i++){
            Row row = sheet.createRow((2+i));

            Cell cell0 = row.createCell(0);
            cell0.setCellValue(list.get(i).getNoticeId());
            cell0.setCellStyle(cssMap.get("css0"));

            Cell cell1 = row.createCell(1);
            cell1.setCellValue(list.get(i).getNoticeContent());
            cell1.setCellStyle(cssMap.get("css0"));

            Cell cell2= row.createCell(2);
            cell2.setCellValue(list.get(i).getNoticeTitle());
            cell2.setCellStyle(cssMap.get("css0"));
        }
        FileOutputStream pis = new FileOutputStream("d:\\0022\\test.xlsx");
        wb.write(pis);
        pis.close();
        String saveName = "【我的作业】记录表(" + DateUtils.getDate()+").xlsx";
        return FileUtil.down("d:\\0022\\test.xlsx",saveName);
    }






}
